<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <div>
    <input type="text" placeholder="请输入文字" id="txt">
  </div>
  <canvas id="myCanvas" width="400" height="400"></canvas>

  <script>
    const myCanvas = document.getElementById('myCanvas');
    const txt = document.getElementById('txt');

    const ctx = myCanvas.getContext('2d');

    txt.oninput = function () {
      let t = this.value;
      console.log(t);

      ctx.clearRect(0, 0, 400, 400);

      ctx.font = '30px 宋体';
      ctx.textAlign = 'center';
      ctx.direction = 'ltr';
      ctx.fillText(t, 50, 50);
    }

    ctx.direction = 'ltr';
    ctx.fillText('tknu shushu', 50, 50);
  </script>
</body>

</html>